<template>
  <div class="app-container">

    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">

      <el-row>
        <el-form-item label="试卷名称:" class="item el-col-5">
          <el-input v-model="searchObj.examName" placeholder="试卷名称" ></el-input>
        </el-form-item>

        <!--<el-form-item label="状态:" class="item el-col-5">-->
          <!--<el-input v-model="searchObj.status" placeholder="试卷状态" ></el-input>-->
        <!--</el-form-item>-->


        <el-form-item label="姓名:" class="item el-col-5">
          <el-input v-model="searchObj.userName" placeholder="姓名" ></el-input>
        </el-form-item>

        <el-form-item label="手机:" class="item el-col-5">
          <el-input v-model="searchObj.userPhone" placeholder="手机" ></el-input>
        </el-form-item>

        <el-button type="primary" icon="el-icon-search" @click="fetchData(pageInfo.pageNum)">查询</el-button>
      </el-row>



      <el-button type="primary" @click="addOpen" style="float:right;">新增</el-button>
    </el-form>

    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      style="width: 100%">

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (pageInfo.pageNum - 1) * pageInfo.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="examName" label="试卷名称"/>
      <el-table-column prop="userName" label="姓名"/>
      <el-table-column prop="userPhone" label="手机"/>
      <el-table-column prop="examStartDate" :formatter="dateFormatter" label="开始时间"/>
      <el-table-column prop="examEndDate" :formatter="dateFormatter" label="结束时间"/>
      <el-table-column prop="timeLimit" label="限时/分钟"/>
      <el-table-column prop="completeDate" :formatter="dateFormatter" label="完成考试时间"/>
      <el-table-column prop="scoreTotal" label="分数"/>
      <el-table-column prop="status" :formatter="statusFormatter" label="状态"/>

      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">

          <el-button size="mini" @click="showDataById(scope.row.id)">查看</el-button>
          <el-button type="danger" :disabled="scope.row.status !== 'noExam'" size="mini" @click="removeDataById(scope.row.id)">删除</el-button>

        </template>
      </el-table-column>

    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="pageInfo.pageNum"
      :total="pageInfo.total"
      :page-size="pageInfo.pageSize"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />


    <!--新增-->
    <el-dialog title="新增" :visible.sync="dialogAddVisible" :close-on-click-modal="false" width="1500px">

      <el-form :inline="true" class="demo-form-inline" label-width="80px">
        <el-row>
          <el-form-item label="试卷名称" label-width="120px" class="el-col-7">
            <el-select v-model="searchAddObj.ceeExamId">
              <el-option
                v-for="item in examNameList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="姓名: " label-width="120px" class="el-col-7">
            <el-input v-model="searchAddObj.userName" placeholder="姓名" ></el-input>
          </el-form-item>

          <el-form-item label="手机: " label-width="120px" class="el-col-7">
            <el-input v-model="searchAddObj.userPhone" placeholder="手机" ></el-input>
          </el-form-item>

        </el-row>

        <el-row>
          <el-form-item class="el-col-7" label="开始时间:" label-width="120px" >
            <el-date-picker
              v-model="searchAddObj.examStartDate"
              type="date"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>

          <el-form-item class="el-col-7" label="结束时间:" label-width="120px" >
            <el-date-picker
              v-model="searchAddObj.examEndDate"
              type="date"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="限时/分钟:" label-width="120px" class="el-col-7">
            <el-input v-model="searchAddObj.timeLimit" type="number" placeholder="限时" ></el-input>
          </el-form-item>
        </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="save">保 存</el-button>
        <el-button @click="dialogAddVisible = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>
<script>

  import * as examUserService from '@/api/examUser';
  import * as selectService from '@/api/select';
  import moment from 'moment';

  export default {
    // 定义数据
    data() {
      return {
        listLoading: true, // 数据是否正在加载
        list: null, // banner列表
        total: 0, // 数据库中的总记录数
        page: 1, // 默认页码
        limit: 10, // 每页记录数

        totalGrade: 0, // 总分

        //search
        searchObj: {},

        //add
        searchAddObj: {},

        dialogAddVisible: false,

        pageInfo: {
          pageSize: 10,
          pageNum: 1,
          total: 0
        },

        examNameList: [],
      }
    },


    // 当页面加载时获取数据
    created() {
      this.fetchData(1);

      this.getExamDataList();
    },


    methods: {
      // 调用api层获取数据库中的数据
      fetchData(currentPageNum) {
        const query = {};
        this.pageInfo.pageNum = currentPageNum;
        query.pageInfo = this.pageInfo;
        query.examName = this.searchObj.examName;

        examUserService.getPage(query).then(response => {
          this.list = response.data.list;
          this.pageInfo.total = response.data.total;
          // 数据加载并绑定成功
          this.listLoading = false
          }
        )
      },

      dateFormatter(row, column, cellValue) {

        var date = row[column.property];

        if (date == undefined) {
          return '';
        }
        return moment(date).format('YYYY-MM-DD');
      },

      statusFormatter(row, column, cellValue){
        var status = row[column.property];
        if (status === 'noExam'){
          return '未考试';
        }
        if (status === 'endExam'){
          return '待打分';
        }
        if (status === 'endScore'){
          return '已打分';
        }
        return '';
      },

      addOpen(){
        this.dialogAddVisible=true;
        this.searchAddObj = {};
      },

      save(){
        const query = {};
        query.ceeExamId = this.searchAddObj.ceeExamId;
        query.userName = this.searchAddObj.userName;
        query.userPhone = this.searchAddObj.userPhone;
        query.examStartDate = this.searchAddObj.examStartDate;
        query.examEndDate = this.searchAddObj.examEndDate;
        query.timeLimit = this.searchAddObj.timeLimit;
        query.status = 'noExam';

        examUserService.addExamUser(query).then(res =>{
          this.fetchData(1);
          this.dialogAddVisible=false;
          //提示
          this.$message({
            type: 'success',
            message: '新增成功!'
          });
        });
      },

      showDataById(id){
        let routeUrl = this.$router.resolve({
          path: "detail",
          query: {id: id}
        });

        console.log(routeUrl.href);
        window.open(routeUrl.href, '_blank');
      },

      //删除
      removeDataById(id) {
        this.$confirm('此操作将删除考试信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => { //确定执行then方法
          //调用接口
          examUserService.deleteExamUser(id)
            .then(response => {
              //提示
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              //刷新页面
              this.fetchData(this.pageInfo.pageNum);
            })
        })
      },

      getExamDataList(){
        selectService.examDataList().then(res =>{

           this.examNameList = res.data;
        });
      },



      // 当页码发生改变的时候
      changeSize(size) {
        console.log(size)
        this.pageInfo.pageSize = size;
        this.fetchData(1);
      },

      // 重置查询表单
      resetData() {
        console.log('重置查询表单')
        this.searchObj = {}
        this.fetchData()
      },

    }
  }
</script>

<style scoped lang="scss">
  .objhead {
    width: 100%;
    /*height: 47px;*/
    background: #effaff;
    line-height: 47px;
    color: #0076ff;
    /*padding-left: 26px;*/
    box-sizing: border-box;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 20px;
    border-bottom:1px solid #000;

  }


</style>
